/**
 * Created by Administrator on 2015/4/27.
 */
(function(window,BaseClass){

	var Calendar = BaseClass.extend({
		init : function(){

		},
		prefix : function(str){
			if(!str) return false;
			str=String(str);
			return str.length==1 ? ("0"+str) : str;
		},
		currentDate : "",
		/**
		 * 取得当前月份
		 */
		month : function(){
			var month = new Date().getMonth()+1;
			return this.prefix(month);
		},
		nextMonth : function(month){
			if(!month) month = new Date().getMonth()+1;
			month = Number(month);
			var nextMonth = month+1;
			return nextMonth>12 ? "01" : this.prefix(nextMonth);
		},
		prevMonth : function(month){
			if(!month) month = new Date().getMonth()+1;
			month = Number(month);
			var prevMonth = month-1;
			return prevMonth<1 ? "12" : this.prefix(prevMonth);
		},
		/**
		 * 取得当前时间 2014-04-26
		 */
		today : function(){
			var date = new Date();
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			m = this.prefix(m);
			var d = date.getDate();
			d = this.prefix(d);
			return y+"-"+m+"-"+d;
		},
		/**
		 * 基于当前日期的下一天或指定某天的下一天
		 * @param date  2014-04-26
		 * @returns {string}
		 */
		nextDay : function(date){
			if(date){
				date = new Date(date);
			}else{
				date = new Date();
			}
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			var a = new Date(y,m,0);
			var maxDay = a.getDate();
			var nextday = d+1;
			if(nextday>maxDay){
				nextday = "01";
				m++;
				if(m>12){
					m = "01";
					y++;
				}
			}
			nextday = this.prefix(nextday);
			m = this.prefix(m);
			return y+"-"+m+"-"+nextday;
		},
		/**
		 * 基于当前日期的前一天或指定某天的前一天
		 * @param date  2014-04-26
		 * @returns {string}
		 */
		prevDate : function(date){
			if(date){
				date = new Date(date);
			}else{
				date = new Date();
			}
			var date = new Date();
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			var a = new Date(y,m,0);
			var prevdate = d-1;
			if(prevdate<1){
				prevdate = "01";
				m++;
				if(m>12){
					m = "01";
					y++;
				}
			}
			prevdate = this.strpad(prevdate);
			m = this.strpad(m);
			return y+"-"+m+"-"+prevdate;
		},
		getCurrentDate : function(){
			return this.currentDate;
		},
		setCurrentDate : function(date){
			if(!date) return false;
			this.currentDate = String(date);
		},
		outputHtml : function(yearmonth){
			this.setCurrentDate(yearmonth);
			var nowtime=new Date();
			nowtime.setHours(0,0,0,0);
			nowtime=nowtime.getTime();//凌晨时间
			yearmonth = yearmonth.substring(0,8);
			var begintime=yearmonth+"-01";
			begintime=begintime.split("-");
			begintime=begintime.join("/");
			var beginDate=new Date(begintime);
			var year=beginDate.getFullYear();
			var month=beginDate.getMonth()+1;
			var lastmonth=(month-1==0)?12:month-1;
			var nextmonth=(month+1==13)?1:month+1;
			var days=(new Date(beginDate.getFullYear(),beginDate.getMonth()+1,0)).getDate();
			var monthdays=days;
			var emptydays=beginDate.getDay();
			var endtime=yearmonth+"-"+days;
			endtime=endtime.split("-");
			endtime=endtime.join("/");
			var endDate=new Date(endtime);
			days+=beginDate.getDay()+(7-endDate.getDay());
			beginDate.setTime(beginDate.getTime()-(24*3600000*beginDate.getDay()));
			var nextday = new Date(this.today()).getTime().toString();
			var lastmonth_none="lastmonth_none";
			var nextmonth_none="nextmonth_none";
			var html=	'<div class="title"><span class="lastmonth '+lastmonth_none+'"><a class="prevMonthBtn monthBtn" href="#" title="'+lastmonth+'月">&nbsp;</a></span><span class="nextmonth '+nextmonth_none+'"><a class="nextMonthBtn monthBtn" href="#" title="'+nextmonth+'月">&nbsp;</a></span><span class="year" data-date="'+year+'-'+month+'">'+year+'年'+month+'月</span><span class="close closeCalendarBtn"></span></div>'+
				'<table>'+
				'<tr>'+
				'<th class="weeken">日</th>'+
				'<th>一</th>'+
				'<th>二</th>'+
				'<th>三</th>'+
				'<th>四</th>'+
				'<th>五</th>'+
				'<th class="weeken">六</th>'+
				'</tr>';
			for(var i=0,j=0;i<days-1;i++){
				if(i%7==0){
					html+='<tr>';
				}
				var date=beginDate.getFullYear()+"-"+this.prefix((beginDate.getMonth()+1))+"-"+this.prefix(beginDate.getDate());
				var price="";
				var priceT="";
				var valid="";
				var validT="";
				var remain;
				if(i<emptydays||i>=monthdays+emptydays){
					html+='<td class="day day-empty"><div class="detail"></div></td>';
				}else if(beginDate.getTime()<nowtime){
					html+='<td class="day day-valid"><div class="detail valid" data-date="'+date+'"><span>'+beginDate.getDate()+'</span></div></td>';
					//html+='<td class="day day-empty"><div class="detail"><span>'+beginDate.getDate()+'</span></div></td>';
				}else{
					html+='<td class="day day-valid"><div class="detail valid" data-date="'+date+'"><span>'+beginDate.getDate()+'</span></div></td>';
				}
				if(i%7==6){
					html+='</tr>';
				}
				beginDate.setTime(beginDate.getTime()+24*3600000);
			}
			html+="</table>";
			return html;
		},
		nextMonthOutput : function(){
			var curDate = this.getCurrentDate();
			curDate = curDate.split("-");
			var year = curDate[0];
			var month = curDate[1];
			var day = curDate[2];
			var nextDate = "";
			var nextMonth = this.nextMonth(month);
			if(nextMonth<Number(month)) year++;
			nextDate = year+"-"+nextMonth+"-"+day;
			return this.outputHtml(nextDate);
		},
		prevMonthOutput : function(){
			var curDate = this.getCurrentDate();
			curDate = curDate.split("-");
			var year = curDate[0];
			var month = curDate[1];
			var day = curDate[2];
			var prevDate = "";
			var prevMonth = this.prevMonth(month);
			if(prevMonth>Number(month)) year--;
			prevDate = year+"-"+prevMonth+"-"+day;
			return this.outputHtml(prevDate);
		}
	})

	window["CalendarCore"] = window["CalendarCore"] || Calendar;

})(window,BaseClass);














